﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using Common;
using Common.Tools;
using Common.Model;

using MyGameServer.Manager;
using MyGameServer.Model;
using System.IO;
using System.Xml.Serialization;

namespace MyGameServer.Handler
{
    class LoginHandler : BaseHandler
    {
        public LoginHandler()
        {
            OpCode = OperationCode.Login;
        }
        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
        {
            string username = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.Username) as string;
            string password = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.Password) as string;
            UserManager manager = new UserManager();
            
            bool isSuccess = manager.VerifyUser(username, password);
            OperationResponse response = new OperationResponse(operationRequest.OperationCode);
            
            if (isSuccess)
            {
                User user = manager.GetByUsername(username);
                peer.uuid = user.Id;
                peer.username = username;
                peer.peerList = MyGameServer.Instance.peerList;

                response.ReturnCode = (short)Common.ReturnCode.Success;
                
                CommonUser myUser = new CommonUser();
                myUser.id = user.Id;
                myUser.username = username;


                Dictionary<byte, object> data_myUser = new Dictionary<byte, object>();
                
                string myUserString = Functions.ObjectToXmlString<CommonUser>(myUser);
                data_myUser.Add((byte)ParameterCode.Login, myUserString);

                //string roomListString = Functions.ObjectToXmlString<List<Room>>(MyGameServer.Instance.roomList);
                //data_myUser.Add((byte)ParameterCode.RoomList, roomListString);

                response.Parameters = data_myUser;
                peer.SendOperationResponse(response, sendParameters);



                //
                //for (int i = 0; i < MyGameServer.Instance.peerList.Count; i++)
                //{
                //    ClientPeer p = MyGameServer.Instance.peerList[i];

                //    CommonUser commonUser = new CommonUser();
                //    MyGameServer.Instance.onlineUserList.Add(commonUser);
                //    commonUser.id = p.uuid;
                //    commonUser.username = p.username;
                //}

                //MyGameServer.log.Info("MyGameServer.Instance.commonUserList: " + MyGameServer.Instance.onlineUserList);

                //StringWriter sw = new StringWriter();
                //XmlSerializer serializer = new XmlSerializer(typeof(List<CommonUser>));
                //serializer.Serialize(sw, MyGameServer.Instance.onlineUserList);
                //sw.Close();
                //string usernameListString = sw.ToString();
                
                //for (int i = 0; i < MyGameServer.Instance.peerList.Count; i++)
                //{
                //    ClientPeer tempPeer = MyGameServer.Instance.peerList[i];
                //    if (string.IsNullOrEmpty(tempPeer.username) == false)
                //    {
                //        EventData ed = new EventData((byte)EventCode.Login);

                //        Dictionary<byte, object> data = new Dictionary<byte, object>();
                //        data.Add((byte)ParameterCode.Login, usernameListString);

                //        ed.Parameters = data;
                //        tempPeer.SendEvent(ed, sendParameters);
                //    }
                //}
            }
            else
            {
                response.ReturnCode = (short)Common.ReturnCode.Failed;
                peer.SendOperationResponse(response, sendParameters);
            }
            
        }
    }
}
